﻿namespace WMS6.Web.UserControls
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class Pager : UserControl
    {
        protected HtmlInputHidden hdnCurrentPage;
        protected HtmlInputHidden hdnPageSize;
        protected HtmlInputHidden hdnSortDirection;
        protected HtmlInputHidden hdnSortField;
        protected HtmlInputHidden hdnTotalPages;
        protected Label lblPageInfo;
        protected LinkButton lnkbtnFirstPage;
        protected LinkButton lnkbtnGoTo;
        protected LinkButton lnkbtnLastPage;
        protected LinkButton lnkbtnNextPage;
        protected LinkButton lnkbtnPrePage;
        protected TextBox txtGo2Page;

        public event userEvent First;

        public event userEvent GoTo;

        public event userEvent Last;

        public event userEvent Next;

        public event userEvent Previous;

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            if (this.First != null)
            {
                this.First(this, e);
            }
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            if (this.Last != null)
            {
                this.Last(this, e);
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (this.Next != null)
            {
                this.Next(this, e);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            if (this.Previous != null)
            {
                this.Previous(this, e);
            }
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (this.GoTo != null)
            {
                this.GoTo(this, e);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public int CurrentPage
        {
            get
            {
                return int.Parse(this.hdnCurrentPage.Value);
            }
            set
            {
                this.hdnCurrentPage.Value = value.ToString();
            }
        }

        public int GoToPage
        {
            get
            {
                int temp = 0;
                if (!string.IsNullOrEmpty(this.txtGo2Page.Text.Trim()))
                {
                    if (int.Parse(this.txtGo2Page.Text.Trim()) <= this.TotalPages)
                    {
                        temp = int.Parse(this.txtGo2Page.Text) - 1;
                    }
                    return temp;
                }
                return 0;
            }
            set
            {
                this.txtGo2Page.Text = value.ToString();
            }
        }

        public string PageInfo
        {
            get
            {
                return this.lblPageInfo.Text;
            }
            set
            {
                string info = value;
                if ((info[0] == '1') && ((info[4] == '0') && (info[info.Length - 1] == '0')))
                {
                    info = "0" + info.Substring(1, info.Length - 1);
                }
                this.lblPageInfo.Text = info;
            }
        }

        public int PageSize
        {
            get
            {
                return int.Parse(this.hdnPageSize.Value);
            }
            set
            {
                this.hdnPageSize.Value = value.ToString();
            }
        }

        public string SortDirection
        {
            get
            {
                return this.hdnSortDirection.Value;
            }
            set
            {
                this.hdnSortDirection.Value = value;
            }
        }

        public string SortField
        {
            get
            {
                return this.hdnSortField.Value;
            }
            set
            {
                this.hdnSortField.Value = value;
            }
        }

        public int TotalPages
        {
            get
            {
                return int.Parse(this.hdnTotalPages.Value);
            }
            set
            {
                this.hdnTotalPages.Value = value.ToString();
            }
        }

        public delegate void userEvent(object sender, EventArgs arg);
    }
}

